package com.swas.service.impl;

import com.swas.dao.PermDao;
import com.swas.dao.RolePermDao;
import com.swas.dao.UserDao;
import com.swas.entity.*;
import com.swas.dao.RoleDao;
import com.swas.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Transactional
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private RolePermDao rolePermDao;

    @Autowired
    private PermDao permDao;

    @Override
    public List<Role> selectAll() {
        return roleDao.selectByExample(null);
    }

    @Override
    public Integer selectSomeById(Integer id) {
        User user = userDao.selectByPrimaryKey(id);
        return user.getRoleid();
    }

    @Override
    public Integer updateRoleList(Integer id, Integer role) {
        User user = new User();
        user.setId(id);
        user.setRoleid(role);
        int i = userDao.updateByPrimaryKeySelective(user);
        return role;
    }

    @Override
    public List<RolePerm> selectByRoleId(Integer id) {
        RolePermExample example = new RolePermExample();
        example.createCriteria().andRoleidEqualTo(id);
        return rolePermDao.selectByExample(example);
    }

    @Override
    public List<Perm> selectByUid(Integer id) {
        User user = userDao.selectByPrimaryKey(id);
        List<Perm> rootList = permDao.selectRootById(user.getRoleid());
        List<Perm> permissionList = permDao.selectById(user.getRoleid());
        tree(rootList,permissionList);
        return rootList;
    }

    @Override
    public List<Perm> selectById(Integer id) {
        User user = userDao.selectByPrimaryKey(id);
        List<Perm> perms = permDao.selectRootById(id);
        List<Perm> permissionList = permDao.selectById(user.getRoleid());
        tree(perms,permissionList);
        return perms;
    }

    @Override
    public boolean updateRole(Integer id, Integer[] ids) {
        RolePermExample example = new RolePermExample();
        example.createCriteria().andRoleidEqualTo(id);
        rolePermDao.deleteByExample(example);
        for (int i = 0; i < ids.length; i++){
            RolePerm rolePerm = new RolePerm();
            rolePerm.setRoleid(id);
            rolePerm.setPermid(ids[i]);
            int insert = rolePermDao.insert(rolePerm);
        }
        return true;
    }

    private void tree(List<Perm> rootList, List<Perm> permissionList) {
        for (Perm root : rootList){
            List<Perm> children = new ArrayList<>();
            for (Perm child : permissionList){
                if(child.getParentid().equals(root.getId())){
                    children.add(child);
                }
            }
            if(children.size()>0){
                tree(children,permissionList);
            }
            root.setChildren(children);
        }
    }

}
